package com.mecol.library.web;

import com.mecol.library.entity.Book;
import com.mecol.library.entity.BookClass;
import com.mecol.library.entity.BookSearch;
import com.mecol.library.entity.UserSearch;
import com.mecol.library.service.BookClassService;
import com.mecol.library.service.BookService;
import com.mecol.library.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {


    @Autowired
    private BookService bookService;

    @Autowired
    private BookClassService bookClassService;


    @RequestMapping("/bookList")
    public String userList(HttpSession session)
    {

        List<BookClass> bookClasses=bookClassService.getAllBookClasses();
        session.setAttribute("classes",bookClasses);
        return "/jsp/book/bookList";
    }



    @RequestMapping("/getAllBookList")
    @ResponseBody
    public ResultUtil getAllBookList(Integer page, Integer limit, BookSearch search)
    {
        return bookService.getAllBookList(page,limit,search);
    }



    @RequestMapping("/addBook")
    public String addAdmin(HttpSession session)
    {
        List<BookClass> bookClasses=bookClassService.getAllBookClasses();
        session.setAttribute("classes",bookClasses);
        return "/jsp/book/addBook";
    }

    @RequestMapping("/addNewBook")
    @ResponseBody
    public ResultUtil addNewBook(Book book)
    {
     //   System.out.println(book);

        bookService.addNewBook(book);
        return ResultUtil.ok();
    }


    @RequestMapping("/editBook/{book_id}")
    public String editStudent(@PathVariable("book_id")int book_id, HttpSession session)
    {
        Book book=bookService.getBookById(book_id);
        session.setAttribute("book",book);
        List<BookClass> bookClasses=bookClassService.getAllBookClasses();
        session.setAttribute("classes",bookClasses);
        return "/jsp/book/editBook";
    }

    @RequestMapping("/updateBook")
    @ResponseBody
    public ResultUtil updateBook(Book book)throws ParseException
    {
        //System.out.println("曹操。。。。。。");

        return bookService.updateBook(book);
    }


    @RequestMapping("/deleteBookById")
    @ResponseBody
    public ResultUtil deleteBookById(int book_id)
    {
        return bookService.deleteBookById(book_id);
    }


}
